package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.ManBean;
import com.project.mapper.IManMapper;
import com.project.service.IManService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class ManServiceImpl extends BaseService implements IManService {
    @Override
    public int add(ManBean man) {
        //得到会话对象
        SqlSession session = this.getSession();
        //得到mapper接口实现类对象
        IManMapper mapper = session.getMapper(IManMapper.class);
        //调用mapper持久方法，完成添加
        mapper.add(man);
        session.commit();
        session.close();
        return man.getId();
    }

    @Override
    public void addList(List<ManBean> manList) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.addList(manList);
        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void delMore(int[] idArray) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.delMore(idArray);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, LocalDate birthday) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.update(id,birthday);
        session.commit();
        session.close();
    }

    @Override
    public List<ManBean> findAll() {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        List<ManBean> list = mapper.findAll();

        session.close();
        return list;
    }

    @Override
    public ManBean findById(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper =  session.getMapper(IManMapper.class);
        ManBean man = mapper.findById(id);
        session.close();
        return man;
    }

    @Override
    public List<ManBean> findByName(String name) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        List<ManBean> list = mapper.findByName(name);
        session.close();
        return list;
    }

    @Override
    public List<ManBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        List<ManBean> list =  mapper.findByItem(name,startDate,endDate);
        session.close();
        return list;
    }

    @Override
    public int countMan() {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        int count = mapper.countMan();
        session.close();
        return count;
    }

    @Override
    public CutPageBean<ManBean> cutAll(int pageNO) {
        CutPageBean<ManBean> cutBean = new CutPageBean<ManBean>();
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        //得到当前页的数据
        List<ManBean> list = mapper.cutAll((pageNO - 1) * CutPageBean.PAGESIZE ,
                CutPageBean.PAGESIZE);
        cutBean.setList(list);

        //得到总记录数
        cutBean.setCount(mapper.countMan());

        session.close();
        return cutBean;
    }

    @Override
    public CutPageBean<ManBean> cutByItem(int pageNO, String name, LocalDate startDate, LocalDate endDate) {
        CutPageBean<ManBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        List<ManBean> list = mapper.cutByItem(name,startDate,endDate,
                (pageNO - 1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE);
        cutBean.setList(list);

        cutBean.setCount(mapper.countByItem(name,startDate,endDate));

        session.close();
        return cutBean;
    }
}
